import java.util.HashMap;
import java.util.Map;

class Solution {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4};
        System.out.println(longestSubsequence(arr , 1));
    }
    public static int longestSubsequence(int[] arr, int difference) {
        int len = arr.length ;
        int[] dp = new int[len];
         int sum = 0;
        Map<Integer,Integer> map = new HashMap<>() ;
        dp[0] = 1 ;
        map.put(arr[0] , 1) ;
        for(int i=1 ; i < len ; i ++){
           dp[i] = 1 ;
           dp[i] = Math.max(dp[i] , map.getOrDefault(arr[i]-difference , 0)+1);
           sum = Math.max(sum , dp[i]);
           map.put(arr[i] , dp[i]);
        }
       return sum ;
    }
}